using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Timers;

using MediaPortal.GUI.Library;
using MediaPortal.Dialogs;
using MediaPortal.Util;
using MediaPortal.Localisation;
using MediaPortal.Configuration;
using MediaPortal.Player;
using MediaPortal.Playlists;



namespace MTVMusicPlugin
{
  public class MTVMusicGUIBase : GUIWindow
  {
    public Settings _setting = new Settings();
    protected PlayListPlayer playlistPlayer;

    public void SetLabels(VideoEntry vid, string type)
    {
      //ClearLabels(type);
      //GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Video.Title", vid.Video.Title);
      //GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Video.Year", vid.Video.CopyrightYear.ToString());
      //GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Video.Rating", vid.Video.Rating.ToString());
      //GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Video.Image", GetLocalImageFileName(vid.Image.Url));
      //GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Artist.Name", vid.Artist.Name);
      //GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Artist.Image", GetLocalImageFileName(provider.GetArtistImage(vid.Artist.Id,400)));
      //if (vid.Albums.Count > 0)
      //  GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Album1.Title", vid.Albums[0].Release.Title);
      //if (vid.Albums.Count > 1)
      //  GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Album2.Title", vid.Albums[1].Release.Title);
      //if (vid.Categories.Count > 0)
      //{
      //  GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Category1.Title", vid.Categories[0].Name);
      //  GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Category1.Type", vid.Categories[0].Type.ToString());
      //}
      //if (vid.Categories.Count > 1)
      //{
      //  GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Category2.Title", vid.Categories[1].Name);
      //  GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Category2.Type", vid.Categories[1].Type.ToString());
      //}
    }

    public void ClearLabels(string type)
    {
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Video.Title", " ");
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Video.Year", " ");
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Video.Rating", " ");
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Artist.Name", " ");
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Artist.Image", " ");
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Album1.Title", " ");
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Album2.Title", " ");
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Category1.Title", " ");
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Category1.Type", " ");
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Category2.Title", " ");
      GUIPropertyManager.SetProperty("#YahooMusic." + type + ".Category2.Type", " ");
    }

    public string FormatTitle(VideoEntry vid)
    {
      //if (string.IsNullOrEmpty(_setting.Format_Title))
      //  return vid.Video.Title;
      //string s = _setting.Format_Title;
      //s = s.Replace("%title%", vid.Video.Title);
      //s = s.Replace("%artist%", vid.Artist.Name);
      //s = s.Replace("%year%", vid.Video.CopyrightYear.ToString());
      //s = s.Replace("%rating%", vid.Video.Rating.ToString());
      return string.Format("{0} - {1}", vid.Entry.Author, vid.Entry.FullName);
    }
    
    public string GetLocalImageFileName(string strURL)
    {
      if (strURL == "")
        return string.Empty;
      string url = String.Format("mtvmusiccache-{0}.jpg", Utils.EncryptLine(strURL));
      return System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), url); ;
    }

    public bool filterVideoContens(VideoEntry vid)
    {
      //if (_setting.ShowPlayableOnly && string.IsNullOrEmpty(vid.Video.Id))
      //  return false;
      return true;
    }

    public void Err_message(int langid)
    {
      GUIDialogOK dlgOK = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);
      if (dlgOK != null)
      {
        dlgOK.SetHeading(25660);
        dlgOK.SetLine(1, langid);
        dlgOK.SetLine(2, "");
        dlgOK.DoModal(GetID);
      }
    }

    public void DoPlay(VideoEntry vid)
    {
      if (vid != null)
      {
        //SetLabels(vid, "NowPlaying");
        if (!string.IsNullOrEmpty(vid.PlayblackUrl))
        {
          g_Player.PlayBackStopped += new g_Player.StoppedHandler(g_Player_PlayBackStopped);
          g_Player.Play(vid.PlayblackUrl);
          g_Player.ShowFullScreenWindow();
        }
        else
        {
          Err_message(128071);
        }
      }
    }

    void g_Player_PlayBackStopped(g_Player.MediaType type, int stoptime, string filename)
    {
      g_Player.Release();
      g_Player.PlayBackStopped -= g_Player_PlayBackStopped;
      ClearLabels("NowPlaying");
    }

    public void AddItemToPlayList(GUIListItem pItem)
    {
      playlistPlayer = PlayListPlayer.SingletonPlayer;
      PlayList playList = playlistPlayer.GetPlaylist(PlayListType.PLAYLIST_VIDEO);
      AddItemToPlayList(pItem, ref playList);
    }

    public void AddItemToPlayList(GUIListItem pItem, ref PlayList playList)
    {
      if (playList == null || pItem == null)
        return;
      //TODO
      List<GUIListItem> list = new List<GUIListItem>();
      VideoEntry vid = pItem.MusicTag as VideoEntry;
      if (vid != null)
      {
        list.Add(pItem);
        PlayListItem playlistItem = new PlayListItem();
        playlistItem.Type = PlayListItem.PlayListItemType.Video;// Playlists.PlayListItem.PlayListItemType.Audio;

        playlistItem.FileName = vid.PlayblackUrl;
        playlistItem.Description = pItem.Label;
        
        playlistItem.Duration = pItem.Duration;
        playlistItem.MusicTag = pItem.MusicTag;
        playList.Add(playlistItem);
      }
    }

  }
}
